package com.hskn.hss.module.operaterecord.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.operaterecord.entity.OperateRecord;
import com.hskn.hss.module.operaterecord.mapper.OperateRecordMapper;
import com.hskn.hss.module.operaterecord.service.IOperateRecordService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Service
public class OperateRecordServiceImpl extends ServiceImpl<OperateRecordMapper, OperateRecord> implements IOperateRecordService {

    @Resource
    private OperateRecordMapper operateRecordMapper;

    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        QueryWrapper<OperateRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(OperateRecord::getCreateTime);
        String createUid = params.getString("createUid");
        if (StringUtils.isNotEmpty(createUid)) {
            queryWrapper.lambda().eq(OperateRecord::getCreateUid, createUid);
        }
        String createName = params.getString("createName");
        if (StringUtils.isNotEmpty(createName)) {
            queryWrapper.lambda().eq(OperateRecord::getCreateName, createName);
        }
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(OperateRecord::getCreateTime, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<OperateRecord> jxqsWorkGridDetailPage = new Page<>(page, size, true);
        IPage<OperateRecord> iPage = operateRecordMapper.selectPage(jxqsWorkGridDetailPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("list", iPage.getRecords());
        return json;
    }

}
